/**
 * Created by Ji.wenjiang on 2016/8/31.
 */

angular.module('inspinia')
  .controller('payMoneyDetail', function ($scope, users, $state, myToaster, SweetAlert,$log) {
    init();
    function init() {

      $scope.patient = JSON.parse(localStorage.getItem('payMoneyMsg'));
      $scope.totalNum = $scope.patient.totalNum;//总次数
      $scope.treatNum = $scope.patient.treatNum;//累计总计付费
      $scope.presentTreatNum = $scope.treatNum;//当前总计付费数
      $scope.treatNum_use = $scope.patient.treatNum_use;//当前付费治疗次数
      $scope.treatNum_nouse = $scope.patient.treatNum_nouse;//当前付费未治疗次数
      $scope.colsNum = 5;
      // $scope.beforNum = $scope.treatNum;
      $scope.presentDrawbackNum = $scope.patient.drawbackNum;//当前退款次数
      $scope.drawbackNum = $scope.patient.drawbackNum;//总计退款次数
     
      $scope.presentNum = $scope.totalNum - $scope.treatNum;
      // $scope.presentNum=10;
      $scope.rowsNum = Math.ceil($scope.totalNum / $scope.colsNum);
      $scope.refund = false;
      // $scope.surplus = $scope.totalNum % $scope.colsNum;
      // console.log($scope.surplus);
      // console.log($scope.rowsNum);

      // if ($scope.treatNum_use != 0) {
      //   var trs = document.getElementById("table1").getElementsByTagName("tr");
      //   var count_treatNum_use = 0;
      //   for (i = 0; i < $scope.rowsNum; i++) {
      //     for (j = 0; count_treatNum_use < $scope.treatNum_use; j++) {
      //      console.log(trs);
      //
      //       count_treatNum_use++;
      //     }
      //   }
      // }

      // $scope.rows.length=$scope.rowsNum;
    }

    initTable();
    function initTable() {
      $scope.rows = [];
      $scope.cols = [];

      function caculate(i, j) {
        var index = i * $scope.colsNum + j + 1;
        if (index <= $scope.treatNum_use) {
          return 1;//已缴费已执行
        }
        if (index > $scope.treatNum_use && index <= ($scope.treatNum_nouse + $scope.treatNum_use)) {
          // &&( !$scope.refund?(index <($scope.treatNum- $scope.presentNum)):true)

          return 2;//已缴费未执行
        }
        if (index > ($scope.treatNum_nouse + $scope.treatNum_use) && index <= ($scope.treatNum_use + $scope.treatNum_nouse + $scope.presentDrawbackNum)) {
          return 4;//已退费
        }
        if (index > ($scope.treatNum_use + $scope.treatNum_nouse + $scope.presentDrawbackNum) && index <= $scope.totalNum) {
          return 3;//未缴费
        }

      }

      for (var i = 0; i < $scope.rowsNum; i++) {
        var cols = [];
        for (var j = 0; j < $scope.colsNum; j++) {
          cols[j] = caculate(i, j);
        }
        $scope.rows[i] = cols;
        $scope.rows[i].index = i;
        // console.log($scope.rows.length);
        // var dataRow = {index: i};
        // $scope.rows.push(dataRow);
      }
    }


    $scope.addModelMessage = function (type) {
      switch (type) {
        case 1:
          modelPay();
          break;
        case 2:
          CTpay();
          break;
        case 3:
          coutourPay();
          break;
        case 4:
          treatPay();
          break;
        case 5:
          refund();//切换付费缴费
        default:
          break;
      }
      function modelPay() {
        SweetAlert.swal({
            title: "温馨提示",
            text: "确定缴费吗",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定缴费",
            cancelButtonText: "不，我再想想",
            closeOnConfirm: false,
            closeOnCancel: false
          },
          function (isConfirm) {
            if (isConfirm) {
              SweetAlert.swal("缴费成功", "", "success");

            } else {
              SweetAlert.swal("取消删除", "", "error");
            }
          });

      }

      function CTpay() {
        SweetAlert.swal({
            title: "温馨提示",
            text: "确定缴费吗",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定缴费",
            cancelButtonText: "不，我再想想",
            closeOnConfirm: false,
            closeOnCancel: false
          },
          function (isConfirm) {
            if (isConfirm) {
              SweetAlert.swal("缴费成功", "", "success");

            } else {
              SweetAlert.swal("取消删除", "", "error");
            }
          });
      }

      function coutourPay() {
        SweetAlert.swal({
            title: "温馨提示",
            text: "确定缴费吗",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定缴费",
            cancelButtonText: "不，我再想想",
            closeOnConfirm: false,
            closeOnCancel: false
          },
          function (isConfirm) {
            if (isConfirm) {

              SweetAlert.swal("缴费成功", "", "success");

            } else {
              SweetAlert.swal("取消删除", "", "error");
            }
          });
      }

      function treatPay() {
        $scope.refundType = $scope.refund == false ? '缴费' : '退费';


        SweetAlert.swal({
            title: "温馨提示",
            text: "您确定<" + $scope.refundType + ">吗",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "确定缴费",
            cancelButtonText: "不，我再想想",
            closeOnConfirm: false,
            closeOnCancel: false
          },
          function (isConfirm) {
            if (isConfirm) {
              //缴费操作
              if ($scope.refund == false) {
               // $log.debug($scope.treatNum,$scope.presentNum)
                if ($scope.presentNum <=$scope.totalNum-$scope.treatNum_use-$scope.treatNum_nouse) {
                  // $scope.treatNum = $scope.presentNum + $scope.beforNum;
                  // $scope.presentTreatNum = $scope.treatNum;
                  // $scope.treatNum_nouse = $scope.presentTreatNum + $scope.presentNum;
                  $scope.treatNum = $scope.treatNum + $scope.presentNum;
                  if ($scope.presentNum >= $scope.presentDrawbackNum) {
                    $scope.treatNum_nouse = $scope.treatNum_nouse + $scope.presentNum;
                    $scope.presentDrawbackNum = 0;
                    if ($scope.treatNum_use + $scope.treatNum_nouse >= $scope.totalNum) {
                      $scope.payFlag = true;
                    }
                  }
                  if ($scope.presentNum > 0 && $scope.presentNum < $scope.presentDrawbackNum) {
                    $scope.treatNum_nouse = $scope.treatNum_nouse + $scope.presentNum;
                    $scope.presentDrawbackNum = $scope.presentDrawbackNum - $scope.presentNum;
                    // console.log("缴费时的nouse："+$scope.treatNum_nouse);
                    // console.log("缴费时的drawNUM："+$scope.presentDrawbackNum);
                  }

                  initTable();

                  SweetAlert.swal("缴费成功", "", "success");
                  // if ($scope.treatNum >= $scope.totalNum) {
                  //   $scope.treatNum = $scope.totalNum;
                  //   $scope.presentTreatNum = $scope.treatNum;
                  //   $scope.treatNum_nouse=$scope.totalNum-$scope.treatNum_use;
                  //   $scope.payFlag = true;
                  //   SweetAlert.swal("缴费成功，已达到最大次数", "", "success");
                  // }
                  // // $scope.treatNum=$scope.treatNum>20?20:$scope.treatNum;
                  // $scope.beforNum = $scope.treatNum;
                  // $scope.treatNum_nouse = $scope.treatNum - $scope.treatNum_use;
                  // console.log("增加：" + $scope.treatNum_nouse);
                } else {
                  SweetAlert.swal("缴费失败", "输入次数已超过可缴费次数", "error");
                }
              }
              if ($scope.refund == true) {
                if ($scope.presentNum <= $scope.treatNum_nouse) {
                  $scope.drawbackNum = $scope.drawbackNum + $scope.presentNum;
                  $scope.treatNum_nouse = $scope.treatNum_nouse - $scope.presentNum;
                  $scope.presentDrawbackNum = $scope.presentDrawbackNum + $scope.presentNum;
                  if ($scope.treatNum_nouse == 0) {
                    $scope.payFlag = true;
                  }
                  console.log("退费时的nouse：" + $scope.treatNum_nouse);
                  console.log("退费时的drawNUM：" + $scope.presentDrawbackNum);
                  initTable();
                  // $scope.treatNum = $scope.treatNum - $scope.presentNum;
                  SweetAlert.swal("退费成功", "", "success");
                } else {
                  SweetAlert.swal("退费失败", "输入次数已超过可退费次数", "error");
                }
                // if ($scope.presentNum >= $scope.treatNum_nouse) {
                //   // $scope.treatNum = $scope.treatNum - $scope.presentNum;
                //   $scope.drawbackNum = $scope.presentNum;
                //   $scope.totalDrawback = $scope.treatNum_nouse;
                //   // $scope.treatNum_nouse = $scope.treatNum - $scope.treatNum_use;
                //   initTable();
                //   $scope.payFlag = true;
                //   SweetAlert.swal("退费成功，已全部退还", "", "success");
                // }
                // // $scope.treatNum = $scope.treatNum - $scope.presentNum;
                // // $scope.treatNum_nouse=$scope.treatNum-$scope.treatNum_use;
              }

              // $scope.beforNum = $scope.treatNum;
              // console.log("当前：" + $scope.totalDrawback);

            } else {
              SweetAlert.swal("取消删除", "", "error");
            }
          });

      }

      function refund() {
        $scope.refund = !$scope.refund;
        if ($scope.refund == true) {
          if ($scope.treatNum_nouse != 0) {
            $scope.payFlag = false;
          }
          $scope.presentNum = $scope.treatNum_nouse;
        }
        if ($scope.refund == false) {
          if ($scope.totalNum > $scope.treatNum_use + $scope.treatNum_nouse) {
            $scope.payFlag = false;
          }
          $scope.presentNum = $scope.totalNum - $scope.treatNum_use - $scope.treatNum_nouse;
        }
      }


    }
  })
